use "${clean}newspaper_analysis.dta", clear
keep if north == 1 | south == 1
drop if year >= 1900

// recoding Alexandria, VA to the South (technically part of DC until 1844)
replace south = 1 if city == "Alexandria"
replace north = 0 if city == "Alexandria"
replace state = "Virginia" if city == "Alexandria"

// dropping DC as a border area
drop if state == "District Of Columbia"

// standardizing variables
egen county_surban = std(county_urban_percent)
egen county_srugged = std(county_ruggedness)
egen county_spostoffice = std(county_postoffice_area)

// keeping the sample consistent
cap drop sample
gen sample = 1
foreach var of varlist $county_npcontrols {
	replace sample = . if `var' == .
}
keep if sample == 1



// predicting yhat for north and south
lowess singular year if north == 1, nograph bwidth(.6) generate(yhat_n)
lowess singular year if south == 1, nograph bwidth(.6) generate(yhat_s)



// collapsing
collapse (mean) yhat* singular (count) weight = singular, by(year north south)

cap drop weight_bubble
gen weight_bubble = .
replace weight_bubble = 1 if weight >= 1 & weight <= 10
replace weight_bubble = 2 if weight > 10 & weight <= 25
replace weight_bubble = 3 if weight > 25 & weight <= 50
replace weight_bubble = 4 if weight > 50



// taking the first differences of all of the lowess lines
sort north year
gen dys = yhat_s - yhat_s[_n-1]

sort south year
gen dyn = yhat_n - yhat_n[_n-1]



// graphing the lowess lines against the bubbles
twoway ///
	(scatteri 1 1860.5 1 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri -.05 1860.5 -.05 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatter singular year if north== 1 & weight_bubble == 1,  mcolor("${blue}") msym(Oh) msize(small) ) ///
	(scatter singular year if north== 1 & weight_bubble == 2,  mcolor("${blue}") msym(Oh) msize(medium) ) ///
	(scatter singular year if north== 1 & weight_bubble == 3,  mcolor("${blue}") msym(Oh) msize(large) ) ///
	(scatter singular year if north== 1 & weight_bubble == 4,  mcolor("${blue}") msym(Oh) msize(huge) ) ///
	(line yhat_n year, lwidth(thick) lpattern(solid) lcolor("${blue}")) ///
	, legend(ring(0) pos(11) row(6) order(3 "1-10" 4 "11-25" 5 "26-50" 6 ">50") region(lcolor(gs1))) ///
	ytitle("") ///
	subtitle("Northern Newspapers", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	xscale(off) ///
	ylab(, grid glcolor(gs7%50)) ///
	name(north_bubble, replace) nodraw

	
twoway ///
	(scatteri 1 1860.5 1 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri -.05 1860.5 -.05 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatter singular year if south== 1 & weight_bubble == 1,  mcolor("${red}") msym(Oh) msize(small) ) ///
	(scatter singular year if south== 1 & weight_bubble == 2,  mcolor("${red}") msym(Oh) msize(medium) ) ///
	(scatter singular year if south== 1 & weight_bubble == 3,  mcolor("${red}") msym(Oh) msize(large) ) ///
	(scatter singular year if south== 1 & weight_bubble == 4,  mcolor("${red}") msym(Oh) msize(huge) ) ///
	(line yhat_s year, lwidth(thick) lpattern(dash) lcolor("${red}")) ///
	, legend(off) /// 
	subtitle("Southern Newspapers", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	ytitle("") ///
	xscale(off) ///
	ylab(, grid glcolor(gs7%50)) ///
	name(south_bubble, replace) nodraw	

	
// graphing the derivate lines against the year
twoway ///
	(scatteri .015 1860.5 .015 1865.5, bcolor(gs10%25) recast(area)) ///
	(scatteri 0 1860.5 0 1865.5, bcolor(gs10%25) recast(area)) ///
	(line dyn year, lwidth(medthick) lpattern(solid) lcolor("${blue}")) ///
	(line dys year, lwidth(medthick) lpattern(dash) lcolor("${red}")) ///
	, legend(order(3 "Northern Newspapers" 4 "Southern Newspapers") ring(0) pos(10) row(2) region(lcolor(gs1))) ///
	ytitle("") ///
	xlab(1800(20)1900) ///
	xtitle("") ///
	subtitle("Year-on-Year Change", box bexpand bcolor(gs2) color(white) size(medlarge) ) ///
	ylab(, grid glcolor(gs7%50)) ///
	graphregion(margin(l=-5)) ///
	name(deriv, replace) nodraw

graph combine north_bubble south_bubble deriv, col(1) xsize(20) ysize(35) l2title("Perc. Singular") b2title("Year of Publication", size(medsmall))
graph export "${output}Fig1_np_ns_bubbles_acceleration.pdf", as(pdf) replace

